(function() {
    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }
    var from = 0;
    var appid = GetQueryString("appid");
    var ucp_id = GetQueryString("ucp_id");
    var phone = GetQueryString("phone");
    var price = GetQueryString("amount");
    var from = GetQueryString("from");

    var tel = phone.substring(0, 3) + "****" + phone.substr(phone.length - 4);
    $('.phonetext1').html(tel);

    var headers = {
            'appid': appid,
            'token': '',
            'version': 'v1', //版本号
            'fromsource': 5, //来源 
            'device': 'wechat', //设备
            'content-type': 'application/x-www-form-urlencoded'
        }
        // 测试
        // var request = 'https://n.qikeya.com/erp_api/public/index.php/';
        // 正式
    var request = 'https://api-erp.qikeya.cn/';


    // 发送验证码
    $('.sendtime').html('发送验证码');

    // 点击发送验证码
    $('.sendtime').click(function() {
        var tel = $('#phone').val();
        if ($('.sendtime').hasClass('sendtimeactive')) {

        } else {
            $('.showerViplo').show();
            $('.sendtime').addClass('sendtimeactive');
            var currentTime = 60;
            $('.sendtime').html(currentTime + 's重新发送');
            var time = setInterval(function() {
                currentTime--;
                $('.sendtime').html(currentTime + 's重新发送');
                if (currentTime <= 0) {
                    clearInterval(time);
                    $('.sendtime').html('发送验证码');
                    $('.sendtime').removeClass('sendtimeactive')
                }
            }, 1000)
            $.ajax({
                url: request + 'crmapi/pay/SmsSurpluPay',
                data: {
                    phone: phone,
                    ucp_id: ucp_id,
                    amount: price,
                    from: from,
                },
                headers: headers,
                dataType: "json",
                success: function(res) {
                    if (Number(res.errcode) == 0) {
                        layer.open({
                            content: '发送成功',
                            skin: 'msg',
                            time: 1 //1秒后自动关闭
                        });

                    } else {
                        layer.open({
                            content: res.msg,
                            skin: 'msg',
                            time: 1 //1秒后自动关闭
                        });
                    }
                    $('.showerViplo').hide();
                }
            })
        }
    })
    $('.submitbut').click(function() {
        var tel = $('#phone').val();
        var message = $('#code').val();
        if ($('.submitbut').hasClass('submitbutactive')) {

        } else {
            if (message == '') {
                layer.open({
                    content: '请输入验证码',
                    skin: 'msg',
                    time: 1 //1秒后自动关闭
                });
            } else {
                $('.showerViplo').show();
                $('.submitbut').addClass('submitbutactive');
                var obj = {
                    ucp_id: ucp_id,
                    amount: price,
                    code: message,
                    phone: phone,
                    from: from
                }
                $.ajax({
                    type: "GET",
                    url: request + 'crmapi/pay/surpluPay',
                    data: obj,
                    headers: headers,
                    dataType: "json",
                    success: function(res) {
                        if (Number(res.errcode) == 0) {
                            var data = res.data;
                            sessionStorage.setItem('payis', 1);
                            $('.detailsuccess').fadeIn();
                        } else {
                            $('.submitbut').removeClass('submitbutactive')
                            layer.open({
                                content: res.msg,
                                skin: 'msg',
                                time: 1 //1秒后自动关闭
                            });
                        }
                        $('.showerViplo').hide();
                    }
                })
            }
        }
    })
})();